<?

class Smutty_Smarty {

	function getFieldName( $str ) {

		$parts = explode( '.', $str );

		return sizeof($parts) == 1
			? $parts[0] : $parts[0] . '[' . $parts[1] . ']';

	}

	/**
	 *  draws an HTML element
	 *
	 *  @param [params] parameters
	 *  @param [smarty] smarty object
	 *
	 */

	function htmlElement( $name, $attrs, $smarty ) {

		$smarty->depend( 'modifier', 'escape' );

		$name = strtolower( $name );
		$singles = array( 'link', 'input', 'hr', 'br' );
		$isSingle = false;

		// is it a single element?
		foreach ( $singles as $single )
			if ( $single == $name ) {
				$isSingle = true;
				break;
			}

		// build the attribute string
		$attrString = '';
		foreach ( $attrs as $key => $value )
			if ( $value )
				$attrString .= ' ' . $key . '="' . smarty_modifier_escape($value) . '"';

		// now put the element together
		$elem = '<' . $name . $attrString;
		if ( $isSingle )
			$elem .= ' />';
		else
			$elem .= '>';

		return $elem;

	}

	function hidden( $params, $smarty ) {

		return Smutty_Smarty::htmlElement( 'input', array(
			type => 'hidden',
			name => Smutty_Smarty::getFieldName($params['name']),
			value => $params['value']
		), $smarty );

	}

	function button( $params, $smarty ) {

		$urlHash = Smutty_Utils::strToHash( $params['url'] );
		$url = Smutty_Utils::getUrl( $urlHash );
		$update = $params['update'];
		$handler = $params['handler'];

		return Smutty_Smarty::htmlElement( 'input', array(
			type => 'button',
			value => $params['text'],
			onclick => 'javascript:smutty_ajaxForm(' .
					'this.form,' .
					'\'' . $url . '\',' .
					'\'' . $update . '\',' .
					'\'' . $handler . '\'' .
				');',
			'class' => $params['class'],
			id => $params['id']
		), $smarty );

	}

	function field( $params, $smarty ) {

		$field = '';

		// add a label?
		if ( $label = $params['label'] ) {
			$smarty->depend( 'function', 'label' );
			$field .= Smutty_Smarty::label(array(
				'for' => $params['name'],
				text => $label,
				'class' => $params['labelClass']
			), $smarty );
		}

		// is it a "live" field?
		$liveField = '';
		if ( $params['url'] && $params['update'] )
			$liveField = 'smutty_ajaxLiveFieldChange(' .
				'\'' . Smutty_Utils::getUrl(Smutty_Utils::strToHash($params['url'])) . '\',' .
				'\'' . $params['update'] . '\',' .
				'this,' .
				'\'' . $params['feedback'] . '\'' .
			');';

		return $field . Smutty_Smarty::htmlElement( 'input', array(
			name => Smutty_Smarty::getFieldName($params['name']),
			type => ( $params['type'] ? $params['type'] : 'text' ),
			'class' => $params['class'],
			maxlength => $params['maxlength'],
			value => $params['value'],
			onkeyup => $liveField,
			id => $params['id']
		), $smarty );

	}

	function label( $params, $smarty ) {

		$smarty->depend( 'modifier', 'escape' );

		return Smutty_Smarty::htmlElement( 'label', array(
			'for' => Smutty_Smarty::getFieldName($params['for']),
			'class' => $params['class']
		), $smarty ) .
		smarty_modifier_escape( $params['text'] ) .
		'</label>';

	}

	function password( $params, $smarty ) {

		$params['type'] = 'password';

		return Smutty_Smarty::field( $params, $smarty );

	}

	function submit( $params, $smarty ) {

		return Smutty_Smarty::htmlElement( 'input', array(
			type => 'submit',
			value => $params['text']
		), $smarty );

	}

	function textarea( $params, $smarty ) {

		$smarty->depend( 'modifier', 'escape' );

		$textarea = '';

		// add a label?
		if ( $label = $params['label'] ) {
			$smarty->depend( 'function', 'label' );
			$textarea .= Smutty_Smarty::label(array(
				'for' => $params['name'],
				text => $label,
				'class' => $params['labelClass']
			), $smarty );
		}

		return $textarea . Smutty_Smarty::htmlElement( 'textarea', array(
			name => Smutty_Smarty::getFieldName($params['name']),
			'class' => $params['class'],
			id => $params['id']
		), $smarty ) .
		smarty_modifier_escape( $params['value'] ) .
		'</textarea>';

	}

}

?>